import { createRouter, createWebHistory } from 'vue-router';
import Login from '../Login.vue';
import Register from '../Register.vue';
import HomeView from '../HomeView.vue'; // �����µ� HomeView

const routes = [
    {
        path: '/',
        name: 'Home',
        component: HomeView, // ���������������Ϊ HomeView
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const publicPages = ['/login', '/register'];
    const authRequired = !publicPages.includes(to.path);
    const loggedIn = !!localStorage.getItem('access_token');

    if (authRequired && !loggedIn) {
        // �����Ҫ��֤��δ��¼�����ض��򵽵�¼ҳ
        next('/login');
    } else if ((to.path === '/login' || to.path === '/register') && loggedIn) {
        // ����ѵ�¼�����Է��ʵ�¼��ע��ҳ�����ض�����ҳ
        next('/');
    } else {
        next();
    }
});

export default router; 